﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WebEditBackGround
{
    public class SysClass
    {
        //public static string cx_url_post = "http://127.0.0.1:5000";
        //public static string cx_url_post = "http://192.168.100.149:5004";
        //public static string cx_url_post = "http://120.25.100.120:5001";
        public static string cx_url_post = "http://120.77.39.182:5001";

        //public static string cx_path_imgsave_a = @"d:";
        //public static string cx_path_imgsave_a = @"http://120.25.100.120:8001/201704/";
        public static string cx_path_imgsave_a = @"http://www.cerambath.org/abc/ccc/201704/";


        #region 方法：设置DataGridView的格式
        /// <summary>
        /// 方法：设置DataGridView的格式
        /// 列宽根据内容自动设定，列标题不换行，列标题对齐方式
        /// </summary>
        /// <param name="dgv"></param>
        public void FF_DGV_格式设置(DataGridView dgv)
        {
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dgv.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
            dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //dgv.MultiSelect = false;
            dgv.BackgroundColor = Color.Snow;

        }

        public void FF_DGV_格式设置(DataGridView dgv, bool TF_MultiSelect)
        {
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dgv.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
            dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgv.MultiSelect = TF_MultiSelect;
            dgv.BackgroundColor = Color.Snow;

        } 

        #endregion

        // <summary>
        /// 方法：提交POST请求
        /// </summary>
        /// <param name="postUrl">请求地址</param>
        /// <param name="paramData">请求参数</param>
        /// <returns></returns>
        public string FF_PostWebRequest(string postUrl, string paramData)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public DataTable FF_JsonToDataTable<T>(IEnumerable<T> collection)
        {
            var props = typeof(T).GetProperties();
            var dt = new DataTable();
            dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            if (collection.Count() > 0)
            {
                for (int i = 0; i < collection.Count(); i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        object obj = pi.GetValue(collection.ElementAt(i), null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    dt.LoadDataRow(array, true);
                }
            }
            return dt;
        }

        public void FF_PictureBox显示URL图片(PictureBox Control_PictureBox,string csURL)
        {
            try
            {
                System.Net.WebRequest webreq = System.Net.WebRequest.Create(csURL);
                System.Net.WebResponse webres = webreq.GetResponse();
                using (System.IO.Stream stream = webres.GetResponseStream())
                {
                    Control_PictureBox.Image = Image.FromStream(stream);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
